#ifndef __NETWORK_H__
#define __NETWORK_H__

#include "main.h"
#define SERVER_IP "47.118.19.83"
#define SERVER_PORT 11111
#define BUFFER_SIZE 2048

extern int sockfd;
extern pthread_t recv_thread;
extern int is_connected;
extern pthread_mutex_t match_mutex; // 匹配状态互斥锁

// 添加网络消息类型枚举
enum MessageType
{
    MSG_MATCH_REQUEST = 1, // 匹配请求
    MSG_MATCH_CANCEL,      // 匹配取消
    MSG_MATCH_FOUND,       // 匹配成功
    MSG_MATCH_TIMEOUT,     // 匹配超时
    MSG_MOVE_DATA,         // 走棋数据
    MSG_GAME_OVER,         // 游戏结束
    MSG_DISCONNECT         // 断开连接
};

// 走棋数据结构
struct MoveData
{
    int fromX, fromY;
    int toX, toY;
    PieceType pieceType;
    PieceColor pieceColor;
};

enum MatchState
{
    MATCH_NONE,      // 默认状态
    MATCH_SEARCHING, // 匹配中
    MATCH_SUCCESS    // 匹配成功
};

// 全局变量声明
extern MoveData opponentMove;
extern bool hasOpponentMove;
extern MatchState match_state;

void send_match_request();
void send_cancel_match();
MatchState get_match_state();
void reset_match_state();
void *recv_from_server(void *arg);
int connect_to_server(void);
void disconnect_from_server(void);
void send_move_data(int fromX, int fromY, int toX, int toY, PieceType type, PieceColor color);
void handle_opponent_move(int fromX, int fromY, int toX, int toY, PieceType type, PieceColor color);

#endif